﻿using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;

namespace Nydus.Model
{
    /// <summary>
    /// Extension methods for the ObjectStateManager class
    /// </summary>
    static class ObjectStateManagerExtensions
    {
        /// <summary>
        /// Gets all attached object state entries
        /// </summary>
        /// <param name="stateManager">The object state manager</param>
        /// <returns>All attached object state entries.</returns>
        public static IEnumerable<ObjectStateEntry> GetObjectStateEntries(this ObjectStateManager stateManager)
        {
            return stateManager.GetObjectStateEntries(
                  System.Data.EntityState.Added
                | System.Data.EntityState.Modified
                | System.Data.EntityState.Deleted
                | System.Data.EntityState.Unchanged);
        }

        /// <summary>
        /// Gets all attached object state entries where entity is of specified type.
        /// </summary>
        /// <typeparam name="TEntity">Entity type.</typeparam>
        /// <param name="stateManager">The object state manager.</param>
        /// <returns>All attached object state entries.</returns>
        public static IEnumerable<ObjectStateEntry> GetObjectStateEntries<TEntity>(this ObjectStateManager stateManager)
        {
            return stateManager.GetObjectStateEntries().Where(t => t.Entity is TEntity);
        }
    }
}
